﻿using System;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;

namespace GameName1
{
    class PowerUpController : GameObject
    {
        private double _lastSpawnTime;
        public override void Update(GameTime gameTime)
        {
            var rdm = new Random();
            if (gameTime.TotalGameTime.TotalMilliseconds > _lastSpawnTime + 1000 &&
                GameObjectsManager.GetInstance().GetAllObjectsOfType<PowerUp>().Count == 0)
            {
                _lastSpawnTime = gameTime.TotalGameTime.TotalMilliseconds;
                if (rdm.NextDouble() > 0.5)
                {
                    var types = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.IsSubclassOf(typeof(PowerUp))).ToList();
                    var i = rdm.Next(0, types.Count);

                    var x = rdm.Next(50, (int) (ScreenManager.GetInstance().Resolution.X - 50));
                    object[] parameters =
                    {
                        new Vector2(x, 40)
                    };
                    var powerUp = (PowerUp) Activator.CreateInstance(types[i], parameters); 
                    GameObjectsManager.GetInstance().Instantiate(powerUp);
                }
            }
        }
    }
}
